﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc。@Copy Right 2019
// 文件： GovUser.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using Dapper.Extensions;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Repository;
using OF.Utility;
using System.Text;

namespace CGT.Infrastructure.Service
{

    /// <summary>
    ///用户
    /// </summary>
    public class GovUserService
    {
        protected GovUserRepository repository;

        public GovUserService()
        {
            repository = new GovUserRepository();
        }

        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(GovUser entity)
        {
            entity.CheckCode = GetCheckCode(entity);
            return repository.Insert(entity);
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<GovUser> entitys)
        {
            foreach (var entity in entitys)
            {
                entity.CheckCode = GetCheckCode(entity);
            }
            return repository.Insert(entitys);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(GovUser entity)
        {
            entity.CheckCode = GetCheckCode(entity);
            return repository.Update(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(GovUser entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public GovUser SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }


        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public GovUser Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<GovUser> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        /// <summary>
        /// 根据登陆帐号获取用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public GovUser GetByUserName(string userName)
        {
            return repository.GetByUserName(userName);
        }

        /// <summary>
        /// 获取校验码
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string GetCheckCode(GovUser entity)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(SiteUtility.GetAppSettings("LogCheckCode"));
            sb.Append(entity.UserName);
            sb.Append(entity.PassWord);
            sb.Append(entity.RealName);
            sb.Append(entity.IdentityCode);
            sb.Append(entity.IsPolice);
            sb.Append(entity.UserNumber);
            sb.Append(entity.State);
            sb.Append(entity.UserExpireDate.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.PasswordExpireDate.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.LoginStartTime.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.LoginEndTime.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.AllowIP);
            sb.Append(entity.CurrentLoginIP);
            sb.Append(entity.CurrentLoginTime.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.LastLoginIP);
            sb.Append(entity.LastLoginTime.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.ForceChangePwd);
            sb.Append(entity.IsActive);
            sb.Append(entity.CreateDateTime.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.UpdateTime.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.OperationID);
            sb.Append(entity.RoleId);
            return MD5Helper.MD5Key(sb.ToString());
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public Page<GovUser> GetPages(long pageIndex, long pageSize)
        {
            return repository.GetPages(pageIndex, pageSize);
        }
        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="state">状态</param>
        /// <param name="realName">用户名</param>
        /// <param name="loginName">登录名</param>
        /// <returns></returns>
        public Page<GovUser> GetPages(long pageIndex, long pageSize, short state, string realName, string loginName)
        {
            return repository.GetPages(pageIndex, pageSize, state, realName, loginName);
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        public void BatchLogicDelete(string ids)
        {
            repository.BatchLogicDelete(ids);
        }
        /// <summary>
        /// 判断是否已经存在该用户编码
        /// </summary>
        /// <param name="num">用户编码</param>
        /// <param name="isPolice">是否是警员</param>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public bool CheckUserNumIsExist(string num, short isPolice, int userId)
        {
            GovUser userInfo;
            if (userId > 0)
                userInfo = repository.GetUserByNum(userId, num, isPolice);
            else
                userInfo = repository.GetUserByNum(num, isPolice);
            return userInfo != null;
        }
        /// <summary>
        /// 判断是否已经存在该登录名
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool CheckLoginNameIsExist(string loginName, int userId)
        {
            GovUser userInfo;
            if (userId > 0)
                userInfo = repository.GetUserByLoginName(userId, loginName);
            else
                userInfo = repository.GetUserByLoginName(loginName);
            return userInfo != null;
        }
        /// <summary>
        ///  检查身份证号是否已经使用  1是已存在  0是未存在
        /// </summary>
        /// <param name="idnum">身份证号</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public bool CheckIdentityCodeIsExist(string idnum, int userId)
        {
            GovUser userInfo;
            if (userId > 0)
                userInfo = repository.GetUserByIdentityCode(userId, idnum);
            else
                userInfo = repository.GetUserByIdentityCode(idnum);
            return userInfo != null;
        }
    }

}